<script setup>
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
// , Upload
// import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import {
  userGetInfoService,
  userDelService,
  userDetailService,
  userGpaAddService
} from '@/api/user.js'
import { useUserStore } from '@/stores'
const total = ref(0) // 总条数
const loading = ref(false) // loading状态
const {
  user: { studentId },
  state
} = useUserStore()
const code = state ? false : true //管理员身份判断

// 定义请求参数对象
const params = ref({
  type: '',
  current: 1, // 当前页
  size: 5, // 当前生效的每页条数
  cate_id: '',
  state: ''
})

// 定义学生信息列表
const StudentList = ref([
  // 数据可以由接口传输
])

// 基于params参数，获取学生信息列表
const getArticleList = async () => {
  loading.value = true
  const res = await userGetInfoService(params.value)
  StudentList.value = res.data.data.records
  total.value = res.data.data.total
  loading.value = false
}
getArticleList()

// 处理分页逻辑
const onSizeChange = (size) => {
  // 只要是每页条数变化了，那么原本正在访问的当前页意义不大了，数据大概率已经不在原来那一页了
  // 重新从第一页渲染即可
  params.value.current = 1
  params.value.size = size
  // 基于最新的当前页 和 每页条数，渲染数据
  getArticleList()
}
const onCurrentChange = (page) => {
  // 更新当前页
  params.value.current = page
  // 基于最新的当前页，渲染数据
  getArticleList()
}

// 搜索逻辑 => 按照最新的条件，重新检索，从第一页开始展示
const SearchStudentName = ref()
const onSearch = async () => {
  console.log(SearchStudentName.value)
  const res = await userDetailService(SearchStudentName.value)
  params.value.current = 1 // 重置页面
  StudentList.value = res.data.data
}

// 重置逻辑 => 将筛选条件清空，重新检索，从第一页开始展示
const onReset = () => {
  params.value.current = 1 // 重置页面
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

const articleEditRef = ref()
// 添加逻辑
const onAddArticle = () => {
  articleEditRef.value.open({})
}
// 编辑逻辑
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

// 删除逻辑
const onDeleteArticle = async (row) => {
  // 提示用户是否要删除
  await ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  const articleData = {
    ...row
    // 其他属性...
  }
  await userDelService(articleData)
  ElMessage.success('删除成功')
  // 重新渲染列表
  getArticleList()
}

// 添加分数需要的数据
const dialogVisible = ref(false)
const formModel = ref({
  id: '',
  studentId: '',
  oneGpa: '',
  twoGpa: '',
  threeGpa: '',
  fourGpa: ''
})
// 添加分数逻辑
// const onAddScroe = async (row) => {
//   dialogVisible.value = true
//   formModel.value.id = row.id
//   formModel.value.studentId = row.studentId
// }
const addGpa = async () => {
  console.log(formModel.value)
  await userGpaAddService(formModel.value)
  dialogVisible.value = false
  ElMessage.success('添加成功')
  // 重新渲染列表
  getArticleList()
}

// 添加或者编辑 成功的回调
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，最好渲染最后一页
    const lastPage = Math.ceil((total.value + 1) / params.value.size)
    // 更新成最大页码数，再渲染
    params.value.current = lastPage
  }

  getArticleList()
}
</script>

<template>
  <page-container title="学生信息管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle" :disabled="code"
        >添加学生信息</el-button
      >
    </template>

    <!-- 表单区域 -->

    <el-form inline>
      <el-form-item label="姓名">
        <el-input
          v-model="SearchStudentName"
          placeholder="请输入需要查找的学生姓名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="StudentList" style="width: 100%">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column
        prop="studentId"
        label="学号"
        width="200"
      ></el-table-column>
      <el-table-column
        fixed
        prop="studentName"
        label="姓名"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="studentSex"
        label="性别"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="studentGrade"
        label="学生年级"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="studentClass"
        label="班级"
        width="200"
      ></el-table-column>

      <el-table-column
        prop="identity"
        label="政治面貌"
        width="210"
      ></el-table-column>
      <el-table-column
        prop="department"
        label="所在部门"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="politicalStatus"
        label="双创职位"
        width="250"
      ></el-table-column>

      <el-table-column
        prop="studentTel"
        label="电话号码"
        width="280"
      ></el-table-column>
      <el-table-column
        prop="studentDormitory"
        label="所在宿舍"
        width="280"
      ></el-table-column>
      <el-table-column
        prop="studentCounsellor"
        label="辅导员姓名"
        width="280"
      ></el-table-column>
      <el-table-column
        prop="counsellorTel"
        label="辅导员电话"
        width="280"
      ></el-table-column>
      <!-- <el-table-column
        prop="department"
        label="邮箱"
        width="280"
      ></el-table-column> -->

      <el-table-column fixed="right" label="操作" width="150">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
            :disabled="code && row.studentId !== studentId"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDeleteArticle(row)"
            :disabled="code && row.studentId !== studentId"
          ></el-button>
          <!-- <el-button
            circle
            plain
            type="success"
            :icon="Upload"
            @click="onAddScroe(row)"
            :disabled="row.studentId !== studentId"
          ></el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.current"
      v-model:page-size="params.size"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <el-dialog v-model="dialogVisible" title="添加绩点" width="30%">
      <el-form
        :model="formModel"
        label-width="100px"
        style="padding-right: 30px"
      >
        <el-form-item label="学生学号" prop="name">
          <el-input v-model="formModel.studentId"></el-input>
        </el-form-item>
        <el-form-item label="大一绩点" prop="oneGpa">
          <el-input
            v-model="formModel.oneGpa"
            placeholder="请输入大一绩点"
          ></el-input>
        </el-form-item>
        <el-form-item label="大二绩点" prop="twoGpa">
          <el-input
            v-model="formModel.twoGpa"
            placeholder="请输入大二绩点"
          ></el-input>
        </el-form-item>
        <el-form-item label="大三绩点" prop="threeGpa">
          <el-input
            v-model="formModel.threeGpa"
            placeholder="请输入大三绩点"
          ></el-input>
        </el-form-item>
        <el-form-item label="大四绩点" prop="fourGpa">
          <el-input
            v-model="formModel.fourGpa"
            placeholder="请输入大四绩点"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addGpa"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 添加编辑的抽屉 -->
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>

<style lang="scss" scoped></style>
